package com.skycode7.servlet;

import com.skycode7.bean.NewsBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

@WebServlet("/newsEdit.jsp")
public class NewsEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String newsId = req.getParameter("newsId");
        String newsTitle = req.getParameter("upd_NewsTitle");
        String newsContent = req.getParameter("upd_NewsContent");
        NewsBean newsBean = new NewsBean();//实例化一个新闻对象
        //设置新闻对象属性值
        newsBean.setNewsID(Integer.parseInt(newsId));
        newsBean.setNewsTitle(newsTitle);
        newsBean.setNewsContent(newsContent);
        newsBean.setNewsTime((new Date()).toString());
        newsBean.setAdminName("Admin");
        NewsDao newsDao = new NewsDao();
        int result = newsDao.add(newsBean);
        if (result > 0) {
            //补充知识 跳转到网页news。html中
            resp.sendRedirect("news.html");
        } else {
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}